// HSS.Forms.Docking.InertButtonBase.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       InertButtonBase.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;

namespace HSS.Forms.Docking
{
    internal abstract class InertButtonBase : Control
    {
        protected InertButtonBase()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
			this.DoubleBuffered = true;
		}

		/// <summary>
		/// The image to display
		/// </summary>
        public abstract Bitmap Image
        {
            get;
        }

		/// <summary>
		/// The first color of the Button Hover Color
		/// </summary>
		public virtual Color HoverFillColorBegin
		{
			get { return _fillColor1; }
			set { _fillColor1 = value; }
		} Color _fillColor1 = ControlPaint.LightLight(Color.LightSkyBlue);

		/// <summary>
		/// The second color of the Button Hover Color
		/// </summary>
		public virtual Color HoverFillColorEnd
		{
			get { return _fillColor2; }
			set { _fillColor2 = value; }
		} Color _fillColor2 = ControlPaint.LightLight(Color.SkyBlue);

		protected bool IsMouseOver
        {
            get { return _isMouseOver; }
            private set
            {
                if (_isMouseOver == value)
                    return;

                _isMouseOver = value;
                Invalidate();
            }
		} bool _isMouseOver = false;

        protected override Size DefaultSize
        {
            get { return Resources.DockPane_Close.Size; }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            bool over = ClientRectangle.Contains(e.X, e.Y);
            if (IsMouseOver != over)
                IsMouseOver = over;
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            if (!IsMouseOver)
                IsMouseOver = true;
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (IsMouseOver)
                IsMouseOver = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
			Rectangle rect = new Rectangle(0, 0, Image.Width, Image.Height);
			
			if (IsMouseOver && Enabled)
			{
				Rectangle rectB = Rectangle.Inflate(rect, -1, -1);
				using (LinearGradientBrush brush = new LinearGradientBrush(rectB, HoverFillColorBegin, HoverFillColorEnd, LinearGradientMode.Vertical))
					e.Graphics.FillRectangle(brush, rectB);

				using (Pen pen = new Pen(ControlPaint.LightLight(Color.Blue)))
					e.Graphics.DrawRectangle(pen, Rectangle.Inflate(rectB, 0, 0));
			}

			using (ImageAttributes imageAttributes = new ImageAttributes())
			{
				ColorMap[] colorMap = new ColorMap[2];
				colorMap[0] = new ColorMap();
				colorMap[0].OldColor = Color.FromArgb(0, 0, 0);
				colorMap[0].NewColor = ForeColor;
				colorMap[1] = new ColorMap();
				colorMap[1].OldColor = Image.GetPixel(0, 0);
				colorMap[1].NewColor = Color.Transparent;

				imageAttributes.SetRemapTable(colorMap);

				e.Graphics.DrawImage(
				   Image,
				   new Rectangle(0, 0, 16, 16),
				   0, 0,
				   16,
				   16,
				   GraphicsUnit.Pixel,
				   imageAttributes);
			}

			//base.OnPaint(e);
		}

        public void RefreshChanges()
        {
            if (IsDisposed)
                return;

            bool mouseOver = ClientRectangle.Contains(PointToClient(Control.MousePosition));
            if (mouseOver != IsMouseOver)
                IsMouseOver = mouseOver;

            OnRefreshChanges();
        }

        protected virtual void OnRefreshChanges()
        {
        }
    }
}